import {
	HTTP
} from './utils/http.js';

const loginUrl = 'https://scrm.bkchina.cn/scrm-bk-jd/jdUserController/onLogin';
const authCodeUrl = 'https://scrm.bkchina.cn/scrm-bk-jd/jdTokenController/getAccessToken';
// 获取用户信息
const authUserInfoUrl = 'https://scrm.bkchina.cn/scrm-bk-jd/jdUserController/submitUserInfo?';
// 京东plus会员提交给后台
const isPlusInfoUrl = 'https://scrm.bkchina.cn/scrm-bk-jd/jdUserController/submitIsPlusInfo?';

class Init extends HTTP {

/**
 * 提交是否是京东会员给后台
 */
submitIsPlusInfo(res) {
	let _that = this;
	const tempIsPlusInfoUrl = isPlusInfoUrl + 'accessToken=' + jd.getStorageSync('access_token') + '&openId=' + jd.getStorageSync('openId') + '&unionId=' + jd.getStorageSync('unionId');
	console.log(":url----");
	console.log(tempIsPlusInfoUrl);
	console.log("res---====");
	console.log(res);
	return new Promise((resolve, reject) => {
		const submitIsPlusRes = _that._handleGetUserInfo(tempIsPlusInfoUrl, res);
		submitIsPlusRes.then((res) => {
			console.log(res);
			resolve(res);
		});
	});
}

/**
 * 提交用户信息
 */
submitUserInfo(res) {
	let _that = this;
	const tempAuthUserInfoUrl = authUserInfoUrl + 'accessToken=' + jd.getStorageSync('access_token') + '&openId=' + jd.getStorageSync('openId') + '&unionId=' + jd.getStorageSync('unionId');
	console.log(":url----");
	console.log(tempAuthUserInfoUrl);
	console.log("res---====");
	console.log(res);
	return new Promise((resolve, reject) => {
		const submitUserInfoRe = _that._handleGetUserInfo(tempAuthUserInfoUrl, res);
		submitUserInfoRe.then((res) => {
			console.log(res);
			resolve(res);
		  // jd.setStorageSync('userinfo', ...res);
		});
	});
}

/**
 * 获取用户信息
 */
getJdUserInfo() {
  // 查看是否授权
  let _that = this;
  jd.getSetting({
	  success: function(res) {
		  if(res.authSetting['scope.userInfo']) {
			  jd.getUserInfo({
				  withCredentials: true,
				  success: function(res) {
					  const tempAuthUserInfoUrl = authUserInfoUrl + 'accessToken=' + jd.getStorageSync('access_token') + '&openId=' + jd.getStorageSync('openId') + '&unionId=' + jd.getStorageSync('unionId');
					  console.log("获取用户信息URL；");
					  console.log(tempAuthUserInfoUrl);
					  const userInfo = _that._handleGetUserInfo(tempAuthUserInfoUrl, res);
					  console.log("获取用户信息");
					  userInfo.then((res) => {
						  console.log(res);
						  jd.setStorageSync('userinfo', ...res);
					  });
				  }
			  })	
		  }
	  }
  });
}

// 获取用户信息
_handleGetUserInfo(url, res) {
	return this.request({
		url: url,
		method: 'POST',
		data: {
			...res
		}
	});
}

/**
 * 获取用户accessToken
 */
getJdAccessToken() {
	let _that = this;
	jd.getAuthCode({
	  success: function(res) {
		  const getJdUserAccessToken = _that._handleGetAccessToken(res.code, authCodeUrl);
		  getJdUserAccessToken.then((res) => {
			  if(res && res.access_token) {
				  jd.setStorageSync('access_token', res.access_token)
			  }
		  });
	   }
	})
}

_handleGetAccessToken(code, url) {
	return this.request({
			url: url,
			method: 'GET',
			data: { "code": code }
		});
}

/**
 * 检查会话是否过期，如果过期，那么重新调用login获取用户openId
 */
checkSession(cb) {
	// jd.clearStorage();
	let _that = this;
	jd.checkSession({
		success: function() {
			jd.login({
				success: function(res) {
					const getJdUserInfo = _that.handleGetUserOpenId(res.code, loginUrl);
					getJdUserInfo.then((res) => {
						if(res.code == 200) {
							  if(res.openId) {
								  // openId放入全局变量
								  _that.globalData.openId = res.openId;
								  jd.setStorageSync('openId', res.openId);
								  jd.getAuthCode({
								    success: function(res) {
									  if(res.code) {
										  const getJdUserAccessToken = _that._handleGetAccessToken(res.code, authCodeUrl);
										  getJdUserAccessToken.then((res) => {
											  if(res && res.access_token) {
												  // accessToken放入全局变量
												  _that.globalData.accessToken = res.access_token;
												  jd.setStorageSync('access_token', res.access_token);
												  typeof cb == "function" && cb();
											  }
										  });
									  }
								     }
								  });
							  }
							  if(res.unionId) {
								  jd.setStorageSync('unionId', res.unionId)
							  }
							  
						}
					});
				}
			})
		},
		fail: function() {
			// session_key已经失效了，需要重新执行登陆流程
			jd.login({
				success: function(res) {
					const getJdUserInfo = _that.handleGetUserOpenId(res.code, loginUrl);
					getJdUserInfo.then((res) => {
							if(res.code == 200) {
							  if(res.openId) {
								  jd.setStorageSync('openId', res.openId);
								  jd.getAuthCode({
									success: function(res) {
									  const getJdUserAccessToken = _that._handleGetAccessToken(res.code, authCodeUrl);
									  getJdUserAccessToken.then((res) => {
										  if(res && res.access_token) {
											  jd.setStorageSync('access_token', res.access_token);
											  typeof cb == "function" && cb();
										  }
									  });
									 }
								  });
							  }
							  if(res.unionId) {
								  jd.setStorageSync('unionId', res.unionId)
							  }
							  
						}
					});
				}
			})
		}
	})
}

// 进入登陆流程,主要获取用户openId
handleGetUserOpenId(code, url) {
	return this.request({
			url: url,
			method: 'GET',
			data: { "code": code }
		});
	}
}
  
  
module.exports = {
	Init
}
